package com.ynny.Controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ynny.Monitor.MonitorBlockHandler;
import com.ynny.fallback.SentinelFallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sentinel")
public class SentinelController {
    @GetMapping("/rt")
    @SentinelResource(value = "getRTResource",fallback = "rtFallback",fallbackClass = SentinelFallback.class)
    public String getRT() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "进入 RT";
    }
    @GetMapping("/errorRate")
    @SentinelResource(value = "errorRateResource",fallback = "sentinelFallback",fallbackClass = SentinelFallback.class)
    public String errorRate(){
        String msg = "进入异常比例方法";
        System.out.println(msg);
        // 可以改为模拟异常比例
        if (Math.random() < 0.6) {  // 模拟 60% 异常比例
            int m = 5/0;  // 60% 概率抛异常
        } else {
            int m = 5/1;  // 40% 正常执行
        }
        return msg;
    }
    @GetMapping("/errorCount")
    @SentinelResource(value = "errorCountResource",fallback = "sentinelFallback",fallbackClass = SentinelFallback.class)
    public String errorCount(){
        String msg = "进入异常数方法";
        System.out.println(msg);
        int m  = 5/0;
        return msg;


    }
}
